package com.fagp.mew.cms.utils;

import com.fagp.mew.cms.messages.PageResponse;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLDecoder;
import java.util.ArrayList;

public class UpDownloadUtil {
    
    //根路径
    public static String rootpath = System.getProperty("user.dir");
    
    /**
     * 追加
     */
    public static void txtAdd(String file,String txt) {
        file = rootpath+file;
        OutputStreamWriter fw = null;
        try {
        //如果文件存在，则追加内容；如果文件不存在，则创建文件
            File f=new File(file);
            fw = new OutputStreamWriter(new FileOutputStream(file, true), "utf-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        PrintWriter pw = new PrintWriter(fw);
        pw.println(txt);
        pw.flush();
        try {
            fw.flush();
            pw.close();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 清除
     */
    public static void txtcle(String file) {
        OutputStreamWriter fw = null;
        try {
            //如果文件存在，则追加内容；如果文件不存在，则创建文件
            File f=new File(file);
            fw = new OutputStreamWriter(new FileOutputStream(file, false), "utf-8");
            //fw = new FileWriter(f, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
        PrintWriter pw = new PrintWriter(fw);
        pw.print("");
        pw.flush();
        try {
            fw.flush();
            pw.close();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 不追加
     */
    public static void up(String file,String txt) {
        OutputStreamWriter fw = null;
        try {
            //如果文件存在，则追加内容；如果文件不存在，则创建文件
            File f=new File(file);
            fw = new OutputStreamWriter(new FileOutputStream(file, false), "utf-8");

        } catch (IOException e) {
            e.printStackTrace();
        }
        PrintWriter pw = new PrintWriter(fw);
        pw.println(txt);
        pw.flush();
        try {
            fw.flush();
            pw.close();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static PageResponse download(HttpServletResponse resp, String name, String downloadPath) throws IOException {
        String filePath = null;
        try {
            filePath= URLDecoder.decode(downloadPath,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return new PageResponse(false);
        }

        File file = new File("");
        file = new File(downloadPath);
        if(file.isDirectory()){
            return new PageResponse("无法下载文件夹");
        }
        if(!file.exists()){
            return new PageResponse("无文件");
        }

        name = new String(name.getBytes("GBK"), "ISO-8859-1");
        resp.reset();
        resp.setContentType("application/octet-stream");
        resp.setCharacterEncoding("utf-8");
        resp.setContentLength((int) file.length());
        resp.setHeader("Content-Disposition", "attachment;filename=" + name);
        byte[] buff = new byte[1024];
        BufferedInputStream bis = null;
        OutputStream os = null;
        try {
            os = resp.getOutputStream();
            bis = new BufferedInputStream(new FileInputStream(file));
            int i = 0;
            while ((i = bis.read(buff)) != -1) {
                os.write(buff, 0, i);
                os.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new PageResponse("无文件");
        } finally {
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    public static void bytesOut(String path, ArrayList<Byte> bytes ) throws Exception{

        FileOutputStream stream = new FileOutputStream(rootpath + path,false);

        byte[] o = new byte[bytes.size()];

        for (int i = 0; i < bytes.size(); i++) {
            o[i] = bytes.get(i);
        }

        stream.write(o);
        stream.close();

    }
}
